﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QD.Snapshot.Core.DAL
{
    public class SnapToken
    {
        public static Dictionary<string, object> GetBy360()
        {
            Dictionary<string, object> dic = null;
            string sql = "select top 1 HSPK,QiHooGUID from snap_Token_360 order by id desc";

            using (SqlDataReader dr = SqlHelper2013.ExecuteReader(DAO.conString_Token, CommandType.Text, sql, null))
            {
                if (dr.HasRows) dic = new Dictionary<string, object>();
                while (dr.Read())
                {
                    dic.Add("HSPK", dr["HSPK"]);
                    dic.Add("QiHooGUID", dr["QiHooGUID"]);
                }
            }
            return dic;
        }

        public static Dictionary<string, object> GetBySogou(bool isMobile=false)
        {
            Dictionary<string, object> dic = null;
            string sql = "select top 1 SNUID,SUV from sql_s002.dbo.Token order by adddate desc,sort asc";
            if (isMobile)
                sql = "select top 1 SNUID,SUV from sql_s002.dbo.Token_m order by adddate desc,sort asc";

            using (SqlDataReader dr = SqlHelper2013.ExecuteReader(DAO.conString_Token, CommandType.Text, sql, null))
            {
                if (dr.HasRows) dic = new Dictionary<string, object>();
                while (dr.Read())
                {
                    dic.Add("SNUID", dr["SNUID"]);
                    dic.Add("SUV", dr["SUV"]);
                }
            }
            return dic;
        }


        public static Dictionary<string, object> GetByWeChat() {
            Dictionary<string, object> dic = null;
            string sql = "select top 1 * from sql_s002.dbo.WeixinToken";

            using (SqlDataReader dr = SqlHelper2013.ExecuteReader(DAO.conString_Token, CommandType.Text, sql, null))
            {
                if (dr.HasRows) dic = new Dictionary<string, object>();
                while (dr.Read())
                {
                    dic.Add("SNUID", dr["SNUID"]);
                    dic.Add("SUID", dr["SUID"]);
                    dic.Add("SUV", dr["SUV"]);
                }
            }
            return dic;
        }

        public static int GetByWeChat(string SNUID)
        {
            string sql = string.Format("delete from sql_s002.dbo.WeixinToken where SNUID='{0}'",SNUID);
            return SqlHelper2013.ExecuteNonQuery(DAO.conString_Token, CommandType.Text, sql, null);
        }


        public static Dictionary<string, object> GetBySo()
        {
            Dictionary<string, object> dic = null;
            string sql = "select top 1 * from sql_s002.dbo.SoToken";

            using (SqlDataReader dr = SqlHelper2013.ExecuteReader(DAO.conString_Token, CommandType.Text, sql, null))
            {
                if (dr.HasRows) dic = new Dictionary<string, object>();
                while (dr.Read())
                {
                    dic.Add("HSPK", dr["HSPK"]);
                    dic.Add("QiHooGUID", dr["QiHooGUID"]);
                }
            }
            return dic;
        }

        public static int GetBySo(string HSPK)
        {
            string sql = string.Format("delete from sql_s002.dbo.SoToken where HSPK='{0}'", HSPK);

            return SqlHelper2013.ExecuteNonQuery(DAO.conString_Token, CommandType.Text, sql, null);
        }
    }
}
